package com.iss.wyy.helper;

import com.iss.wyy.annotation.RequestMapping;
import com.iss.wyy.bean.Handler;
import com.iss.wyy.bean.Request;
import com.iss.wyy.common.RequestMethod;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ControllerHelper {
    /**
     *用于存放请求与处理器的映射关系
     */
    private static final Map<Request,Handler> REQUEST_HANDLER_MAP = new HashMap<>();

    static {
        //获取所有的COntroller类
        Set<Class<?>> controllerClassSet = ClassHelper.getControllerClassSet();
        //遍历controoller类
        if(CollectionUtils.isNotEmpty(controllerClassSet)){
            for (Class<?> controllerClass : controllerClassSet){
                //获取类上面的RequestMapping
                String controllerMapping="";
                if(controllerClass.isAnnotationPresent(RequestMapping.class)){
                    controllerClass.getAnnotation(RequestMapping.class);
                    //获取注解
                    RequestMapping controllerRequestMapping = controllerClass.getAnnotation(RequestMapping.class);
                    controllerMapping = controllerRequestMapping.value();
                }
                //获取controller类中定义的方法
                Method[] methods = controllerClass.getDeclaredMethods();
                if (ArrayUtils.isNotEmpty(methods)){
                    //遍历controller类中的方法
                    for (Method method : methods) {
                        //判断是否带有RequestMapping注解
                        if (method.isAnnotationPresent(RequestMapping.class)){
                            //获取注解
                            RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                            //获取类注解和方法注解的(请求路径)
                            String mapping = controllerMapping+requestMapping.value();
                            //获取注解的method（请求方法）
                            RequestMethod[] requestMethods = requestMapping.method();
                            //请求与处理器的映射(支持多方法)
                            if(ArrayUtils.isEmpty(requestMethods)){
                                requestMethods = new RequestMethod[]{RequestMethod.GET, RequestMethod.POST, RequestMethod.CONNECT,
                                                                     RequestMethod.DELETE, RequestMethod.HEAD, RequestMethod.PUT,
                                                                     RequestMethod.TRACE, RequestMethod.OPTIONS};
                            }
                            for (RequestMethod requestMethod:requestMethods) {
                                Request request = new Request(requestMethod,mapping);
                                Handler handler = new Handler(controllerClass,method);
                                REQUEST_HANDLER_MAP.put(request,handler);
                            }
                        }
                    }
                }
            }
        }
     }

    public static Handler getHandler(RequestMethod requestMethod,String requestPath) {
        Request request = new Request(requestMethod,requestPath);
        return REQUEST_HANDLER_MAP.get(request);
    }
}
